var BlogBiz = {
		initialize : function(){

            $('#logout_btn').on('click', function(){
                COMMAJAX.common_ajax({
                    url : "/mind/home/logoutcheck.do",
                    callback_fnc : function(){
                        window.setTimeout('location.reload()', 1000);
                    }
                });
            });

			// Setting Default Date
			BlogBiz.setDefualt();

			
			// Blog Content Handling
			$('.postPage').on("click", function(){
				
				var cnt = parseInt($(this).text());
				
				var paramData = {
						OFFSET : cnt
				};
				
				$('article:not(:eq(0))').remove();
				
				COMMAJAX.common_ajax({
					url : "/mind/join/selectListBlogContents.do"
						,data : paramData
						,callback_fnc :  function(data){
							$.each(data.rows, function(){
								BlogBiz.makeBlogContent(this);
							});
					}
				});
			});
			
			// Log-In Check
			$('#regist_btn').on("click" ,function(){
				
				$('#reg_Name').val($('#RegName').text());
				
				paramData = {
					TITLE : "",
					REG_NAME : $('#reg_Name').val(),
					REG_PLACE : "",
					CONTENTS : $('#contents').val(),
					REG_DATE : $('#reg_date').val(),
					UPD_DATE : $('#reg_date').val()
				};
				
				COMMAJAX.common_ajax({
					url : "/mind/join/registBlog.do"
						,data : paramData
						,callback_fnc :  function(data){
							BlogBiz.makeBlogContent(data.rows);
					}
				});
			});
		},
		setDefualt : function(){
			
			// Setting Default Date
			var now = new Date();
			var day = ("0" + now.getDate()).slice(-2);
			var month = ("0" + (now.getMonth() + 1)).slice(-2);
			var today = now.getFullYear()+"-"+(month)+"-"+(day) ;
			$('#reg_date').val(today);
		},
           makeBlogContent : function(data){
			
			// Get today's current date.
			var now = new Date(data.REG_DATE);
			// Array list of days.
			var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
			// Array list of months.
			var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
			// Calculate the number of the current day in the week.
			var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
			// Calculate four digit year.
			function fourdigits(number){
			    return (number < 1000) ? number + 1900 : number;
			                                }
			// Join it all together
			var today = days[now.getDay()] + ", " +
			        months[now.getMonth()] + " " +
			        date + ", " +
			        (fourdigits(now.getYear())) ;
			
			var cnt = 0;
	        //$(function($, undefined){  
			if(!data.COMMENT_CNT ){
				cnt = data.COMMENT_CNT;
			}
			
			var tag =  '<article class="post">';
				tag += '<div class="meta">';
				tag += '<p>Posted on <span class="time">'+ today +'</span> by <a href="#" class="fn">'+ data.REG_NAME + '</a> with <a href="#" class="comments-link">' + cnt + ' comments</a>.</p>';
				tag += '</div>';
				tag += '<div class="entry">';
				tag += '<p>' + data.CONTENTS + '</p>';
				tag += '</div>';
				tag += '<footer>';
				tag += '<a class="more-link">자세히 보기 </a>';
				tag += '</footer>';
				tag += '<input type="hidden" id="txt_hidden'+ data.BLOG_NO  + '" >';
				tag += '</article>'	;
				
	        $("#temp").after(tag);
	        
	        $('#txt_hidden' + data.BLOG_NO).val(data.BLOG_NO);
	        
	        $('#contents').val("");
	        
	        $('.more-link').unbind('click').bind('click', function(){
				var cnt = $(this).parent().parent().find('input[type="hidden"]').val();
				location.href = "/views/blogpost.do" + "?BLOG_NO=" + cnt;
			});
		}
}

!function($){
	BlogBiz.initialize();	
}(jQuery);